package com.runchamp.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.runchamp.PersonalZone;
import com.runchamp.R;

import org.json.JSONObject;

/**
 * Created by Omri on 05/01/2015.
 */
public class NicknameDialog extends AlertDialog {
    private static final String path = "http://runtobo.appspot.com/nickname";
    public String SuggestName;
    public String userId;
    Intent intent;
    EditText nicknameText;

    public NicknameDialog(Context context) {
        super(context);
        intent = new Intent(getContext(), PersonalZone.class);
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        LayoutInflater inflater = getLayoutInflater();
        View promptView = inflater.inflate(R.layout.dialog_nickname, null);
        nicknameText = (EditText) promptView.findViewById(R.id.nickname);
        nicknameText.setText(SuggestName);
        builder.setMessage(R.string.enterNickname)
                .setView(promptView)
                .setPositiveButton(R.string.OK, null)
                .setNegativeButton(R.string.cancel, null)
                .create();

        setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Button buttonOK = getButton(AlertDialog.BUTTON_POSITIVE);
                Button buttonCancel = getButton(AlertDialog.BUTTON_NEGATIVE);
                buttonOK.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        RequestParams params = new RequestParams();
                        params.add("userId", userId);
                        params.add("nickname", String.valueOf(nicknameText.getText()));
                        AsyncHttpClient client = new AsyncHttpClient();
                        client.post(path, params, new JsonHttpResponseHandler() {
                            @Override
                            public void onSuccess(JSONObject response) {
                                String success = response.optString("success");
                                if (success.equals("failed")) {
                                    Toast.makeText(getContext(), "Nickname is not available, choose again", Toast.LENGTH_SHORT);
                                } else {
                                    dismiss();
                                }
                            }
                        });
                    }
                });
                buttonCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                    }
                });

            }
        });
    }
}
